package com.zretc.service.impl;

import java.util.List;
import java.util.Map;

import com.zretc.dao.UserDao;
import com.zretc.dao.impl.UserDaoImpl;
import com.zretc.entity.CollectionInfo;
import com.zretc.entity.CommentInfo;
import com.zretc.entity.CommentRelation;
import com.zretc.entity.OrderInfo;
import com.zretc.entity.UserInfo;
import com.zretc.service.UserService;

public class UserServiceImpl implements UserService{
	private UserDao userDao =new UserDaoImpl();
    /**
     * 将房源添加到收藏夹
     * @author liumeiqi777
     * @param collectionInfo
     * @return
    */
	@Override
	public boolean saveCollectionInfoByStar(CollectionInfo collectionInfo) {
		int n = userDao.insertCollectionInfo(collectionInfo);
		//若影响行数不为0则收藏成功
		if(n != 0) {
			return true;
		}else {
			return false;
		}
		
	}
	/**
	 * 取消收藏夹中的民宿信息
	 * @author liumeiqi777
	 * @param collectionId
	 * @return
	 */
	@Override
	public boolean removeCollectionInfoByStar(CollectionInfo collectionInfo) {
		
				int	n = userDao.deleteCollectionInfo(collectionInfo);
				//若影响行数不为0则取消收藏成功
				if(n != 0) {
					return true;
				}else {
					return false;
				}
	}
	/**
	 * 对订单添加一级评论
	 * @author liumeiqi777
	 * @param commentInfo
	 * @return
	 */
	@Override
	public boolean saveCommentInfo(CommentInfo commentInfo) {
		int n = userDao.insertCommentInfo(commentInfo);
		if(n != 0) {
			return true;
		}else {
			return false;
		}
	
	}
	/**
	 * 对评论进行回复
	 * @author liumeiqi777
	 * @param commentInfo
	 * @return
	 */
	@Override
	public boolean saveCommentInfoRelation(CommentInfo commentInfo) {
		//调用评论回复的方法将回复的评论添加到评论表
		int n = userDao.insertCommentReply(commentInfo);
		//调用添加评论关系表的方法将该回复对应的评论关系内容添加到关系表
		int m = userDao.insertCommentRelation(commentInfo.getCommentRelation());
		//如果两个都添加成功的话，影响行数不为零，则说明回复成功，则返回true
		if(n != 0 && m != 0) {
			return true;
		}else {
			return false;
		}
	}


	@Override
	public int register(UserInfo userInfo) {
		return userDao.insertUserInfo(userInfo);
	}

	@Override
	public Boolean existsOfUserName(String userName) {
		UserInfo findUserInfoByName = userDao.findUserInfoByName(userName);
		return findUserInfoByName != null ? true : false;
	}

	@Override
	public UserInfo login(String userName, String userPassword) {
		return userDao.findUserInfoByNameAndPwd(userName, userPassword);
	}
	/**
	 * 根据收藏夹Id判断是否在该用户的收藏夹中被收藏
	 * @author liumeiqi777
	 * @param userId
	 * @param collectionId
	 * @return
	 */
	@Override
	public boolean findIfCollectionByCollectionIdAndUserId(String collectionId, Integer userId) {
		//默认未被收藏
		boolean flag = false;
		//获取该用户所有的收藏房源的id
		List<String> collectionIds = userDao.listCollectionIds(userId);
		for (int i = 0; i < collectionIds.size(); i++) {
			if(collectionId.equals(collectionIds.get(i))) {
				flag = true;//转换为已被收藏
				break;//结束循环
			}
		}
		return flag;
	}
	/**
	 * 根据房间id查询对该房间的所有一级评价
	 * @author liumeiqi777
	 * @param roomId,parentId
	 * @return
	 */
	@Override
	public List<Map<String, String>> findCommentInfos(String roomId) {
		List<Map<String, String>> list = userDao.listCommentInfos(roomId);
		return list;	
	}
	@Override
	public List<Map<String, String>> findChildrenComments(String parentId) {
		List<Map<String, String>> list = userDao.listCommentInfoChildrens(parentId);
		return list;
	}
	
	/**
	 * 修改用户的密码，根据用户的编号
	 * @author 赵懒洋
	 * @param userNo
	 * @param password
	 * @return 影响行数
	 */
	@Override
	public boolean updateUserPassword(String userNo, String password) {
		int n = userDao.updateUserPassword(userNo, password);
		return n > 0 ? true : false;
	}
}
